//
//  HLRtcCallKitDelegateProxy.m
//  YueYue
//
//  Created by hulu001 on 2024/1/9.
//

#import "HLRtcCallKitDelegateProxy.h"

@implementation HLRtcCallKitDelegateProxy


- (instancetype)init
{
    self.weakDelegates = NSHashTable.weakObjectsHashTable;
    return self;
}

- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel
{
    NSArray *allDelegates = self.weakDelegates.allObjects;
    for (NSObject *delegate in allDelegates) {
        NSMethodSignature *sig = [delegate methodSignatureForSelector:sel];
        if (sig) {
            return sig;
        }
    }
    return [NSObject instanceMethodSignatureForSelector:@selector(init)];
}

- (void)forwardInvocation:(NSInvocation *)invocation
{
    NSArray *allDelegates = self.weakDelegates.allObjects;
    for (id<HLRtcCallKitDelegate> delegate in allDelegates) {
        if ([delegate respondsToSelector:invocation.selector]) {
            [invocation invokeWithTarget:delegate];
        } else {
            invocation.target = nil;
            [invocation invoke];
        }
    }
}

- (void)addDelegate:(id<HLRtcCallKitDelegate>)delegate
{
    [self.weakDelegates addObject:delegate];
}

- (void)removeDelegate:(id<HLRtcCallKitDelegate>)delegate
{
    [self.weakDelegates removeObject:delegate];
}


@end
